package com.uzai.console.service.meraccdetail.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.console.dto.meraccdetail.MerAccDetailQuery;
import com.uzai.console.entity.*;
import com.uzai.console.enums.MerAccDetailSourceSubtypeEnum;
import com.uzai.console.enums.MerAccDetailSourceTypeEnum;
import com.uzai.console.mapper.*;
import com.uzai.console.service.meraccdetail.MerAccDetailService;
import com.uzai.console.vo.meraccdetail.MerAccDetailInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/***
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class MerAccDetailServiceImpl implements MerAccDetailService {

    Logger logger = LoggerFactory.getLogger(MerAccDetailServiceImpl.class);

    @Autowired
    private MerAccDetailMapper merAccDetailMapper;

    /**
     * 查询账户余额交易记录列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merAccDetailQuery
     * @return null
     */
    public Page<MerAccDetailInfoVo> findMerAccDetailList(MerAccDetailQuery merAccDetailQuery){
        //新建返回到页面的LIST对象
        List<MerAccDetailInfoVo> merAccDetailInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<MerAccDetail> page = new Page<>();
        merAccDetailQuery.convert(page);
        ArrayList<MerAccDetail> merAccDetailList =  merAccDetailMapper.findByList(page, merAccDetailQuery);
        if(merAccDetailList != null && merAccDetailList.size() > 0){
            for (MerAccDetail merAccDetail : merAccDetailList){
                MerAccDetailInfoVo merAccDetailInfoVo = new MerAccDetailInfoVo();
                BeanUtils.copyProperties(merAccDetail, merAccDetailInfoVo);
                MerAccDetailSourceTypeEnum merAccDetailSourceTypeEnum = MerAccDetailSourceTypeEnum.getById(merAccDetail.getSourceType());
                if(merAccDetailSourceTypeEnum != null){
                    if(StringUtils.isNotBlank(merAccDetail.getSubtype())){//子交易类型
                        MerAccDetailSourceSubtypeEnum merAccDetailSourceSubtypeEnum = MerAccDetailSourceSubtypeEnum.getByKey(merAccDetail.getSubtype());
                        if(merAccDetailSourceSubtypeEnum != null){
                            merAccDetailInfoVo.setTypeName(merAccDetailSourceTypeEnum.getDesc() + "(" + merAccDetailSourceSubtypeEnum.getDesc() + ")");
                        }else{
                            merAccDetailInfoVo.setTypeName(merAccDetailSourceTypeEnum.getDesc());
                        }
                    }else{
                        merAccDetailInfoVo.setTypeName(merAccDetailSourceTypeEnum.getDesc());
                    }
                }
                merAccDetailInfoVoList.add(merAccDetailInfoVo);
            }
        }
        //返回到页面分页对象
        Page<MerAccDetailInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(merAccDetailInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

}
